<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img{
            width: 200px;
            height: 200px;
            margin-top: 150px;
            margin-left: 500px;
        }
    </style>
    <script>
        window.onload = function(){
            var begin = document.getElementById("begin");
            var si;
            // si用来保存定时器的唯一标识
            begin.onclick = function(){
                // 开启定时器前，先关闭上一个定时器
                clearInterval(si);
                var img = document.getElementById("img");
                var count = 2;
                // 设置定时器
                si = setInterval(function(){
                    img.src = "./img-"+count+".jpg";
                    count++;
                    if (count == 6){
                        count = 1;
                    }
                },1000);
            }
            var end = document.getElementById("end");
            end.onclick = function(){
                clearInterval(si);
            }
        }
    </script>
</head>
<body>
    <img src="./img-1.jpg" id="img">
    <button id="begin">开始</button>
    <button id="end">结束</button>
</body>
</html>